/***
 *      formdata请求
 * let newParams = {}      //  content-type是否是form-data的判断
 if (params && isFormData) {
            newParams = new FormData()
            for (let i in params) {
                newParams.append(i, params[i])
            }
        } else {
            newParams = params
        }
 ****/

import instance from './instance';
import TransCode from "../TransCode";
const request = {}; // 包裹请求方法的容器
// 请求格式/参数的统一
for (let key in TransCode) {
    let api = TransCode[key]; // url method
    // async 作用：避免进入回调地狱
    //params, 请求参数 get：url，put，post，patch（data），delete：url
    // config = {}  配置参数
    request[key]=async function (params,  config = {}) {
        // 不同请求的判断
        let response = {}; // 请求的返回值
        if (api.method === 'put' || api.method === 'post' || api.method === 'patch') {
            try {
                response = await instance[api.method](api.url, params, config)
            } catch (err) {
                response = err
            }
        } else if (api.method === 'delete' || api.method === 'get') {
            config.params = params
            try {
                response = await instance[api.method](api.url, config)
            } catch (err) {
                response = err
            }
        }
        return response; // 返回响应值
    }
}
export default request;
